/*
 * $Id: DateTimeStamp.java 30 2006-06-08 13:26:40Z wjx $
 */
package com.someok.utils.datetime;

/**
 * Generic date time stamp just stores and holds date and time information.
 * This class does not contain any date/time manipulation.
 *
 * <p><a href="DateTimeStamp.java.html"><i>View Source</i></a></p>
 *
 * @version $Revision: 30 $ $Date: 2006-06-08 21:26:40 +0800 (星期四, 08 六月 2006) $
 * @see JDateTime
 */
public class DateTimeStamp implements Comparable, java.io.Serializable {

	private static final long serialVersionUID = 4033992470535696879L;


	/**
	 * Default empty constructor.
	 */
	public DateTimeStamp() {
	}

	/**
	 * Constructor that sets date and time.
	 */
	public DateTimeStamp(int year, int month, int day, int hour, int minute, double second) {
		this.year = year;
		this.month = month;
		this.day = day;
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}

	/**
	 * Constructor that sets just date. Time is set to zeros.
	 */
	public DateTimeStamp(int year, int month, int day) {
		this(year, month, day, 0, 0, 0);
	}

	/**
	 * Year
	 */
	public int year;

	/**
	 * Month, range: [1 - 12]
	 */
	public int month = 1;

	/**
	 * Day, range: [1 - 31]
	 */
	public int day = 1;

	/**
	 * Hour, range: [0 - 23]
	 */
	public int hour;

	/**
	 * Minute, range [0 - 59]
	 */
	public int minute;

	/**
	 * Second, range: [0.000 - 59.999]
	 */
	public double second;


	// ---------------------------------------------------------------- compare

	/**
	 * Compares this object with the specified object for order.  Returns a
	 * negative integer, zero, or a positive integer as this object is less
	 * than, equal to, or greater than the specified object.<p>
	 *
	 * @param   o the Object to be compared.
	 * @return  a negative integer, zero, or a positive integer as this object
	 *		is less than, equal to, or greater than the specified object.
	 *
	 * @throws ClassCastException if the specified object's type prevents it
	 *         from being compared to this Object.
	 */
	public int compareTo(Object o) {
		DateTimeStamp gts = (DateTimeStamp) o;

		int date1 = year * 10000 + month * 100 + day;
		int date2 = gts.year * 10000 + gts.month * 100 + gts.day;

		if (date1 < date2) {
			return -1;
		}
		if (date1 > date2) {
			return 1;
		}

		date1 = hour * 10000000 + minute * 100000 + (int) (second * 1000);
		date2 = gts.hour * 10000000 + gts.minute * 100000 + (int) (gts.second * 1000);

		if (date1 < date2) {
			return -1;
		}
		if (date1 > date2) {
			return 1;
		}

		return 0;
	}


	// ---------------------------------------------------------------- toString

	/**
	 * Simple to string conversion.
	 *
	 * @return date/time string in 'y-m-d h:m:m.s' format
	 */
	public String toString() {
		double sec = (int)(second * 1000) / 1000.0;
		return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + sec;
	}
}
